package com.hongxun.design.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.hongxun.design.R;
import com.hongxun.design.bean.MaterialsBean;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.listeners.MyCheckedChangeListener;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;

/**
 * Created by zerdoor_pc on 2016/5/20.
 */
public class MultiSelecMoreAdapter extends BaseAdapter {
    List<MaterialsBean> itemBean;
    Context context;
    LayoutInflater inflater;
    public Map<Integer, Boolean> map;

    public MultiSelecMoreAdapter(List<MaterialsBean> itemBean, Context context) {
        this.itemBean = itemBean;
        this.context = context;
        Refresh();
        inflater = LayoutInflater.from(context);
    }

    public void Refresh() {
        if (map == null) map = new HashMap<>();
        for (int i = 0; i < itemBean.size(); i++) {
            if (!map.containsKey(i)){
                map.put(i, false);
            }
        }
        notifyDataSetChanged();
    }

    public Object getChecked() {
        List<MaterialsBean> back = new ArrayList<>();
        for (int i = 0; i < itemBean.size(); i++) {
            if (map.get(i)) {
                back.add(itemBean.get(i));
            }
        }
        return back;
    }

    @Override
    public int getCount() {
        return itemBean.size();
    }

    @Override
    public Object getItem(int i) {
        return itemBean.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder = null;
        MaterialsBean bean = itemBean.get(i);
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_multiselectmore, null);
            holder = new Holder(view);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        holder.name.setText(bean.getName());
        holder.code.setText(bean.getSpec());

        holder.flag.setOnCheckedChangeListener(new MyCheckedChangeListener(i) {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked, int position) {
                map.put(position, isChecked);
            }
        });
        holder.multiselect_item.setOnClickListener(new MClickListener(i, holder) {
            @Override
            public void onClick(View view, int position, Object msg) {
                CheckBox textView = (CheckBox) view.findViewById(R.id.multiselect_flag);
                if (textView.isChecked()) {
                    textView.setChecked(false);
                } else {
                    textView.setChecked(true);
                }
            }
        });
        MyLog.L(MString.SUCCESSED + " map=" + JSON.toJSONString(map) + "  i=" + i + "  holder=" + holder);
        try {
            if (map != null && map.get(i) == true) {
                holder.flag.setChecked(true);
            } else {
                holder.flag.setChecked(false);
            }
        } catch (Exception e) {
        }


        return view;
    }


    class Holder {
        TextView name, code;
        LinearLayout multiselect_item;
        CheckBox flag;

        Holder(View view) {
            multiselect_item = (LinearLayout) view.findViewById(R.id.multiselect_item);
            flag = (CheckBox) view.findViewById(R.id.multiselect_flag);
            name = (TextView) view.findViewById(R.id.multiselect_name);
            code = (TextView) view.findViewById(R.id.multiselect_code);
        }
    }
}
